package test.hxzon.blogbackup.baidu;

import org.hxzon.blogbackup.baidu.BlogFile;
import org.hxzon.blogbackup.baidu.BlogWriter;
import org.hxzon.util.DebugUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class FixbugForFileTitle {
    public static void fixPageItem(int index) {
        try {
            Document doc = Jsoup.parse(BlogWriter.readPageInfo(index));
            Element body = doc.body();
            Elements items = body.getElementsByClass("item");
            for (Element item : items) {
                Element title = item.getElementsByClass("tit").first();
                Element category = item.getElementsByClass("cate").first();
                String url = item.getElementsByClass("url").first().html();
                BlogFile file = new BlogFile();
                file.setOrigUrl(url);
                file.setCategory(category);
                file.setTitle(title);
                String fileTotalContent = BlogWriter.readItem(file);
                Document fileDoc = Jsoup.parse(fileTotalContent);
                String bugTitle = fileDoc.title();
                String rightTitle = bugTitle.replaceFirst("<div class=\"tit\">", "").replaceFirst("</div>", "");
                fileDoc.title(rightTitle);
                DebugUtil.debug(fileDoc.html());
            }
        } catch (Exception e) {
            DebugUtil.error("fetch item err," + index + ":" + e.getMessage(), e);
        }
    }

    public static void main(String args[]) {
        DebugUtil.clearDebugClass();
        DebugUtil.addDebugClass(FixbugForFileTitle.class.getCanonicalName());
        for (int i = 244; i >= 244; i--) {
            fixPageItem(i);
        }
    }
}
